package rsj.sql.auth;

import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import rsj.annotation.DynamicSelect;
import rsj.annotation.Param;
import rsj.annotation.Select;
import rsj.service.auth.AuthServiceImpl;

import java.util.List;

/**
 * @author yuanqinglong
 * @since 2021/5/26 9:57
 */
public interface AuthInfoDao {


    /**
     * 计算所有权限个数
     *
     * @return {@link Node}
     */
    @Select("SELECT COUNT(*) FROM sys.sys_authon")
    Node countAllAuth();


    /**
     * 统计个数通过机构名称模糊
     *
     * @param orgName 机构的名字
     * @return {@link Node}
     */
    @Select("SELECT count(*) FROM sys.sys_authon authon LEFT JOIN sys.sys_org org ON authon.aut_org_id = org.org_id " +
            " LEFT JOIN sys.sys_user use ON authon.aut_org_id = use.user_org_id WHERE org.org_sname LIKE #{%orgName%}")
    Node countAuthLikeOrgName(@Param("orgName") String orgName);


    /**
     * 列表
     *
     * @param pageSize 页面大小
     * @param pageNow  页码
     * @param orgName  机构的名字
     * @return {@link TableNode}
     */
    @DynamicSelect(type = AuthServiceImpl.class, method = "authListSql")
    TableNode listByPage(@Param("pageSize") Long pageSize, @Param("pageNow") Long pageNow, @Param("orgName") String orgName);


    /**
     * 查询客户下的账号
     *
     * @param custId cust id
     * @return {@link List <TableNode>}
     */
    @Select("SELECT aut_keyid,aut_id,aut_signkey,aut_secretkey,org_id FROM sys.sys_authon LEFT JOIN sys.sys_org ON org_id = aut_org_id WHERE CONCAT('RSJ',LPAD(org_cust_id::text,4,'0')) = #{custId}")
    TableNode authListByCustomer(@Param(value = "custId") String custId);


    /**
     * 机构的账号标识
     *
     * @param orgId org id
     * @return {@link Node}
     */
    @Select("SELECT aut_keyid FROM sys.sys_authon WHERE aut_org_id = #{orgId}")
    Node getKeyIdByOrganization(@Param(value = "orgId") String orgId);

}
